package com.geeklei.media.ui.common;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.support.v4.view.MotionEventCompat;
import android.text.TextUtils;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import java.util.Locale;

public class AndroidUtils {
    public static float getCurrentBattery(Context context) {
        Intent registerReceiver = context.registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        return ((float) registerReceiver.getIntExtra("level", 0)) / ((float) registerReceiver.getIntExtra("scale", 0));
    }

    public static int getSystemBrightness(Context context) {
        int i = 0;
        try {
            i = System.getInt(context.getContentResolver(), "screen_brightness");
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
        }
        return i;
    }

    public static boolean isAutoAdjustBrightness(Context context) {
        try {
            if (1 == System.getInt(context.getContentResolver(), "screen_brightness_mode")) {
                return true;
            }
            return false;
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void setAutoAdjustBrightness(Context context, boolean z) {
        int i;
        if (z) {
            i = 1;
        } else {
            i = 0;
        }
        System.putInt(context.getContentResolver(), "screen_brightness_mode", i);
    }

    public static float getActivityBrightness(Activity activity) {
        return activity.getWindow().getAttributes().screenBrightness;
    }

    public static void setActivityBrightness(Activity activity, int i) {
        LayoutParams attributes = activity.getWindow().getAttributes();
        attributes.screenBrightness = ((float) i) / 255.0f;
        activity.getWindow().setAttributes(attributes);
    }

    public static void setBoldFontForChinese(TextView textView) {
        textView.getPaint().setFakeBoldText(true);
    }

    public static String getRealFilePathFromContentUri(Context context, Uri uri) {
        Cursor query;
        Exception e;
        if (context == null || uri == null) {
            return null;
        }
        if (uri.getScheme() == null || !uri.getScheme().equals("content")) {
            return null;
        }
        try {
            query = context.getContentResolver().query(uri, new String[]{"_data"}, null, null, null);
            if (query == null) {
                return null;
            }
            try {
                int columnIndexOrThrow = query.getColumnIndexOrThrow("_data");
                query.moveToFirst();
                String string = query.getString(columnIndexOrThrow);
                query.close();
                return string;
            } catch (Exception e2) {
                e = e2;
                e.printStackTrace();
                query.close();
                return null;
            }
        } catch (Exception e3) {
            e = e3;
            query = null;
            e.printStackTrace();
            if (!(query == null || query.isClosed())) {
                query.close();
            }
            return null;
        }
    }

    public static boolean isNetworkConncected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        }
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null ? activeNetworkInfo.isAvailable() : false;
    }

    private static NetworkInfo getActiveNetworkInfo(Context context) {
        if (context == null) {
            return null;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return null;
        }
        return connectivityManager.getActiveNetworkInfo();
    }

    public static boolean isMobileConnected(Context context) {
        return getActiveNetworkInfo(context) == null ? false : false;
    }

    public static boolean isOnlineVideo(Uri uri) {
        if (uri == null) {
            return false;
        }
        String scheme = uri.getScheme();
        if (scheme != null && (scheme.equals("http") || scheme.equals("https") || scheme.equals("rtsp"))) {
            return true;
        }
        scheme = uri.toString();
        if (scheme == null || !scheme.contains("app_smb")) {
            return false;
        }
        return true;
    }

    public static boolean isSmbVideo(Uri uri) {
        if (uri == null) {
            return false;
        }
        String uri2 = uri.toString();
        if (uri2 == null || !uri2.contains("app_smb")) {
            return false;
        }
        return true;
    }

    public static boolean isRtspVideo(Uri uri) {
        if (uri == null) {
            return false;
        }
        String scheme = uri.getScheme();
        if (scheme == null || !scheme.equalsIgnoreCase("rtsp")) {
            return false;
        }
        return true;
    }

    public static boolean isMMSVideo(Uri uri) {
        if (uri == null) {
            return false;
        }
        String host = uri.getHost();
        if (host == null || !host.equalsIgnoreCase("mms")) {
            return false;
        }
        return true;
    }

    public static boolean isWifiConnected(Context context) {
        boolean z = true;
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo == null) {
            return false;
        }
        if (!(activeNetworkInfo.isConnected() && activeNetworkInfo.getType() == 1)) {
            z = false;
        }
        return z;
    }

    @SuppressLint({"NewApi"})
    public static boolean isFreeNetworkConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null || connectivityManager.getActiveNetworkInfo() == null || !connectivityManager.getActiveNetworkInfo().isConnected()) {
            return false;
        }
        if (connectivityManager.isActiveNetworkMetered()) {
            return false;
        }
        return true;
    }

    public static boolean isUseSdk() {
//        if (onlineUri == null || TextUtils.isEmpty(onlineUri.getSdkInfo()) || onlineUri.getSdkdisable()) {
//            return false;
//        }
        return true;
//        return playBySdk(onlineUri.getSdkInfo(), onlineUri.getSdkdisable(), onlineUri.getSource());
    }

    public static boolean playBySdk(String str, boolean z, String str2) {
        if (TextUtils.isEmpty(str) || z) {
            return false;
        }
        return true;
    }

//    public static <T> T[] concat(T[] tArr, T[] tArr2) {
//        if (tArr == null && tArr2 == null) {
//            return null;
//        }
//        if (tArr == null) {
//            return Arrays.copyOf(tArr2, tArr2.length);
//        }
//        if (tArr2 == null) {
//            return Arrays.copyOf(tArr, tArr.length);
//        }
//        T[] copyOf = Arrays.copyOf(tArr, tArr.length + tArr2.length);
//        System.arraycopy(tArr2, 0, copyOf, tArr.length, tArr2.length);
//        return copyOf;
//    }

    public static String formatPercent(long j, long j2) {
        if (j <= 0 || j2 <= 0) {
            return "0.0%";
        }
        float f = (float) j;
        float f2 = (float) j2;
        try {
            return String.format(Locale.US, "%.1f%%", new Object[]{Float.valueOf((f / f2) * 100.0f)});
        } catch (Exception e) {
            e.printStackTrace();
            return "0.0%";
        }
    }

    public static String getLocalIpAddress(Context context) {
        String str = "127.0.0.1";
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            return str;
        }
        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        return (ipAddress & MotionEventCompat.ACTION_MASK) + "." + ((ipAddress >> 8) & MotionEventCompat.ACTION_MASK) + "." + ((ipAddress >> 16) & MotionEventCompat.ACTION_MASK) + "." + ((ipAddress >> 24) & MotionEventCompat.ACTION_MASK);
    }
}
